#!/bin/bash

# Slackware build script for stressapptest

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=stressapptest
VERSION=${VERSION:-1.0.9}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# The -O2 that's normally here is removed, because upstream puts -O3
# after our supplied flags. I assume they know what they're doing.
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-march=i586 -mtune=i686"
  PATCH=yes
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-fPIC"
else
  SLKCFLAGS=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# This patch is based on Debian's support_i586_builds for
# stressapptest-1.0.6. Extra logic here to avoid running autoreconf if
# we don't need to (since it's slow). Allow PATCH=yes in the env for my
# own testing purposes.
if [ "${PATCH:-no}" = "yes" ]; then
  patch -p1 < $CWD/support_i586.diff
  autoreconf -if
fi

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG
gzip -9 $PKG/usr/man/man1/$PRGNAM.1

# There are 2 slightly different copies of the Apache license (COPYING
# and NOTICE). Not being a lawyer, I'm just going to punt and include
# them both in the doc dir. Also, MODULE_LICENSE_APACHE2 looks like it
# belongs in the doc dir, but it's 0 bytes long. *Shrug*.

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING NOTICE *.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
